package com.hailiao.beidou.message.ui.page.chat.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppShape
import com.d10ng.compose.ui.AppText
import com.d10ng.datelib.toDateStr
import com.hailiao.beidou.message.R
import com.hailiao.beidou.message.data.db.constant.SendStatus
import com.hailiao.beidou.message.data.db.entity.MessageEntity
import com.hailiao.beidou.message.ui.view.ContactAvatar

/**
 * 单条消息
 * @Author d10ng
 * @Date 2023/11/22 11:33
 */

// 用户发出的对话冒泡背景颜色
private val selfBubbleColor = Color(0xFFFFDFD5)

// 用户收到的对话冒泡背景颜色
private val otherBubbleColor = Color.White

@Composable
fun MessageView(
    name: String,
    message: MessageEntity,
    onClickLocation: () -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(bottom = 16.dp)
    ) {
        // 时间
        MessageTimeText(message.createTime)
        // 内容
        ContentRow(
            name = name,
            message = message
        )
        // 位置
        if (message.location != null) {
            LocationView(
                self = message.isSendOut,
                location = message.location!!,
                onClick = onClickLocation
            )
        }
    }
}

@Composable
fun MessageTimeText(
    timestamp: Long
) {
    val text = remember(timestamp) {
        timestamp.toDateStr()
    }
    Text(
        text = text,
        style = AppText.Medium.Tips.mini,
        modifier = Modifier
            .fillMaxWidth()
            .padding(bottom = 10.dp),
        textAlign = TextAlign.Center,
        maxLines = 1
    )
}

@Composable
private fun ContentRow(
    name: String,
    message: MessageEntity
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 19.dp)
            .then(
                if (message.isSendOut) Modifier.padding(start = 50.dp)
                else Modifier.padding(end = 50.dp)
            ),
    ) {
        if (message.isSendOut) {
            // 自己发的消息
            Row(
                modifier = Modifier.weight(1f),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.weight(1f))
                // 消息状态
                MessageStatusIcon(
                    modifier = Modifier.padding(end = 14.dp),
                    status = SendStatus.parse(message.status)
                )
                // 内容
                ContentShow(message)
            }
            // 对话三角
            EquilateralTriangle(true)
            // 头像
            ContactAvatar(name = "我")
        } else {
            // 别人发的消息
            // 头像
            ContactAvatar(name = name)
            // 对话三角
            EquilateralTriangle(false)
            // 内容
            Row(
                modifier = Modifier.weight(1f),
                verticalAlignment = Alignment.CenterVertically
            ) {
                ContentShow(message)
                Spacer(modifier = Modifier.weight(1f))
            }
        }
    }
}

@Composable
private fun EquilateralTriangle(
    self: Boolean
) {
    val tint = remember(self) {
        if (self) selfBubbleColor else otherBubbleColor
    }
    val rotate = remember(self) {
        if (self) 180f else 0f
    }
    Icon(
        painter = painterResource(id = R.drawable.message_triangle),
        contentDescription = null,
        modifier = Modifier
            .padding(top = 19.dp)
            .then(
                if (self) Modifier.padding(end = 9.dp)
                else Modifier.padding(start = 9.dp)
            )
            .size(12.dp)
            .rotate(rotate),
        tint = tint
    )
}

@Composable
private fun ContentShow(
    message: MessageEntity
) {
    ContentText(self = message.isSendOut, text = message.content)
}

@Composable
private fun ContentText(
    self: Boolean,
    text: String
) {
    val bubbleColor = remember(self) {
        if (self) selfBubbleColor else otherBubbleColor
    }
    SelectionContainer {
        Text(
            text = text,
            style = AppText.Normal.Title.default,
            modifier = Modifier
                .defaultMinSize(minHeight = 50.dp)
                .background(bubbleColor, AppShape.RC.v6)
                .padding(12.dp)
        )
    }
}

@Composable
private fun LocationView(
    self: Boolean,
    location: MessageEntity.Location,
    onClick: () -> Unit = {},
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 4.dp)
            .then(
                if (self) Modifier.padding(end = 93.dp)
                else Modifier.padding(start = 93.dp)
            ),
        contentAlignment = if (self) Alignment.CenterEnd else Alignment.CenterStart
    ) {
        Text(
            text = location.toShow(),
            style = AppText.Normal.Tips.mini,
            modifier = Modifier
                .background(AppColor.Neutral.line, AppShape.RC.Cycle)
                .clip(AppShape.RC.Cycle)
                .clickable { onClick() }
                .padding(12.dp, 4.dp),
        )
    }
}